/*
 * Created by JFormDesigner on Wed Jun 07 11:14:52 CST 2023
 */

package gui.register;

import dao.UserDao;
import entity.User;
import util.CodeUtil;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class RegisterFrame extends JFrame {
    public RegisterFrame() {
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        userId = new JTextField();
        userName = new JTextField();
        password = new JTextField();
        passwordTemp = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        label6 = new JLabel();
        code = new JTextField();
        rightCode = new JLabel();
        label7 = new JLabel();
        identityCbox = new JComboBox();

        //======== this ========
        setTitle("\u6ce8\u518c\u8d26\u53f7\u9875\u9762");
        setPreferredSize(new Dimension(595, 580));
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u8d26\u53f7");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(120, 75, 115, 40);

        //---- label2 ----
        label2.setText("\u786e\u8ba4\u5bc6\u7801");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(120, 255, 115, 40);

        //---- label3 ----
        label3.setText("\u8bf7\u8f93\u5165\u60a8\u7684\u6ce8\u518c\u4fe1\u606f\uff1a");
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label3);
        label3.setBounds(35, 20, 180, 40);

        //---- label4 ----
        label4.setText("\u59d3\u540d");
        label4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label4);
        label4.setBounds(120, 135, 115, 40);

        //---- label5 ----
        label5.setText("\u7528\u6237\u5bc6\u7801");
        label5.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label5);
        label5.setBounds(120, 195, 115, 40);
        contentPane.add(userId);
        userId.setBounds(235, 75, 260, 45);
        contentPane.add(userName);
        userName.setBounds(235, 135, 260, 45);
        contentPane.add(password);
        password.setBounds(235, 195, 260, 45);
        contentPane.add(passwordTemp);
        passwordTemp.setBounds(235, 255, 260, 45);

        //---- button1 ----
        button1.setText("\u6ce8\u518c");
        button1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(button1);
        button1.setBounds(235, 465, 105, 45);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String userIdTemp = userId.getText();
                String userNameTemp = userName.getText();
                String userPasswordTemp = password.getText();
                String userPasswordTemp2 = passwordTemp.getText();
                int index = identityCbox.getSelectedIndex();
                String[] list = {"教师", "学生"};
                String userIdentityTemp = null;
                for (int i = 0; i < list.length; i++) {
                    if (i == index) {
                        userIdentityTemp = list[i];
                        break;
                    }
                }
                String correctCode = rightCode.getText();
                String codeInput = code.getText();

                if (userIdTemp.length() == 0 || userPasswordTemp.length() == 0) {
                    //校验用户名和密码是否为空
                    JOptionPane.showMessageDialog(null,"验证码不能为空");
                    String codeStr = CodeUtil.getCode();
                    rightCode.setText(codeStr);
                } else if (codeInput.length() == 0) {
                    //校验验证码是否为空
                    JOptionPane.showMessageDialog(null,"用户名或者密码为空");
                    String codeStr = CodeUtil.getCode();
                    rightCode.setText(codeStr);
                } else if (!codeInput.equals(correctCode)) {
                    //检验验证码是否输入正确
                    JOptionPane.showMessageDialog(null,"验证码输入错误");
                    String codeStr = CodeUtil.getCode();
                    rightCode.setText(codeStr);
                } else if (!userPasswordTemp.equals(userPasswordTemp2)) {
                    //检验两次输入的密码是否一致
                    JOptionPane.showMessageDialog(null,"两次输入的密码不一致");
                    String codeStr = CodeUtil.getCode();
                    rightCode.setText(codeStr);
                } else {
                    User user = new User(userIdTemp, userNameTemp, userPasswordTemp, 0.0F, userIdentityTemp);
                    UserDao userDao = new UserDao();

                    boolean result = false;
                    try {
                        result = userDao.addUser(user);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    if (result) {
                        JOptionPane.showMessageDialog(null, "注册成功");
                        String codeStr = CodeUtil.getCode();
                        rightCode.setText(codeStr);
                    } else {
                        JOptionPane.showMessageDialog(null, "注册失败");
                        String codeStr = CodeUtil.getCode();
                        rightCode.setText(codeStr);
                    }
                }
            }
        });

        //---- button2 ----
        button2.setText("\u53d6\u6d88");
        button2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(button2);
        button2.setBounds(390, 465, 105, 45);

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();
                LoginFrame loginFrame = new LoginFrame();
            }
        });

        //---- label6 ----
        label6.setText("\u9a8c\u8bc1\u7801");
        label6.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label6);
        label6.setBounds(120, 380, 115, 40);
        contentPane.add(code);
        code.setBounds(235, 385, 145, 45);

        //---- rightCode ----
        //获取随机验证码
        String codeStr = CodeUtil.getCode();
        //设置内容
        rightCode.setText(codeStr);
        rightCode.setFont(new Font("Ink Free", Font.PLAIN, 28));
        contentPane.add(rightCode);
        rightCode.setBounds(395, 390, 100, 40);

        //绑定鼠标事件
        rightCode.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                String codeStr = CodeUtil.getCode();
                //设置内容
                rightCode.setText(codeStr);
            }
        });

        //---- label7 ----
        label7.setText("\u7528\u6237\u8eab\u4efd");
        label7.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label7);
        label7.setBounds(120, 320, 115, 40);
        contentPane.add(identityCbox);
        identityCbox.setBounds(235, 320, 260, 45);
        identityCbox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));

        identityCbox.addItem("教师");
        identityCbox.addItem("学生");

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JTextField userId;
    private JTextField userName;
    private JTextField password;
    private JTextField passwordTemp;
    private JButton button1;
    private JButton button2;
    private JLabel label6;
    private JTextField code;
    private JLabel rightCode;
    private JLabel label7;
    private JComboBox identityCbox;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) {
        RegisterFrame registerFrame = new RegisterFrame();
    }
}
